﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FlatRedBall;
using FlatRedBall.Math.Geometry;
using FlatRedBall.Input;

namespace Emptiness
{
    public class Tower : PositionedObject
    {
        #region Fields

        private Sprite mVisibleRepresentation;
        private Polygon mCollision;
        private int mass;
        private long timeToLive = 100000;
        private long ticksCreated = 0;


        #endregion

        #region Properties

        public Sprite VisibleRepresentation
        {
            get { return mVisibleRepresentation; }
        }

        public long TimeToLive
        {
            get { return timeToLive; }
        }
        public long TicksCreated
        {
            get { return ticksCreated; }
        }

        public int Mass
        {
            get { return mass; }
            set { mass = value; }
        }



        #region XML Docs
        /// <summary>
        /// The collision of the object - can be used to perform collisions between entities.
        /// </summary>
        #endregion
        public Polygon Collision
        {
            get { return mCollision; }
        }

        #endregion

        #region Methods

        #region XML Docs
        /// <summary>
        /// Creates the Entitity and adds it to the SpriteManager.  Also adds
        /// its components to their respective managers.
        /// </summary>
        /// <remarks>
        /// Be sure to call Destroy when done with this object so it cleans itself up.
        /// </remarks>
        /// <param name="contentManagerName">The content manager name to use for loading data.</param>
        #endregion
        public Tower(string towerSprite, string contentManagerName, int mass)
        {
            SpriteManager.AddPositionedObject(this);
            this.mass = mass;
            mVisibleRepresentation = SpriteManager.AddSprite("Assets/towers/"+towerSprite, contentManagerName);
            mVisibleRepresentation.AttachTo(this, false);

            float pixelsPerUnit = SpriteManager.Camera.PixelsPerUnitAt(mVisibleRepresentation.Z);
            mVisibleRepresentation.ScaleX = .5f * mVisibleRepresentation.Texture.Width / pixelsPerUnit;
            mVisibleRepresentation.ScaleY = .5f * mVisibleRepresentation.Texture.Height / pixelsPerUnit;



            mCollision = Polygon.CreateEquilateral(
                 8, // Number of sides
                 1, // Radius
                 0);// Angle of first point in radians

            mCollision.ScaleBy(mVisibleRepresentation.ScaleX, mVisibleRepresentation.ScaleY);

            mCollision.AttachTo(this, false); // Don't forget to attach to this
            // Add the collision to the ShapeManager if you
            // want it to be visible.  This can help with debugging.
            bool collisionVisible = false;
            if (collisionVisible)
            {
                ShapeManager.AddPolygon(mCollision);
            }

            ticksCreated = DateTime.Now.Ticks;

        }

        #region XML Docs
        /// <summary>
        /// Removes this and all of its components from their respective managers.
        /// </summary>
        #endregion
        public void Destroy()
        {
            SpriteManager.RemovePositionedObject(this);
            SpriteManager.RemoveSprite(mVisibleRepresentation);
            ShapeManager.Remove(mCollision);
        }

        #endregion


        public void Activity()
        {
            InputManager.Keyboard.ControlPositionedObject(this);
        }

    }
}
